fix potential memory leaks in ggv_ovl. (#456)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Sat, 11 Jan 2020 20:04:49 +0000 (13:04 -0700)
committerGitHub <noreply@github.com>
Sat, 11 Jan 2020 20:04:49 +0000 (13:04 -0700)
* fix potential memory leaks in ggv_ovl.

* ggv_ovl control flow and string processing tweaks.

ggv_ovl.cc
inifile.cc
inifile.h

index 4ba62397344e58b834fba6bcddaea4225e62d463..7eb70d84c80c7906ab4564e4f4ec36babf9e341a 100644 (file)
@@ -21,9 +21,9 @@
  */
 
 #include <cmath>            // for sin, cos, acos
-#include <cstdio>           // for snprintf
 
 #include <QtCore/QString>   // for QString
+#include <QtCore/QVector>   // for QVector
 #include <QtCore/QtGlobal>  // for foreach
 
 #include "defs.h"
@@ -101,10 +101,9 @@ ggv_ovl_read()
 {
   int symbols = inifile_readint_def(inifile, "Overlay", "Symbols", -1);
 
-  for (int i = 1; i <= symbols; i++) {
-    char symbol[32];
+  for (int i = 1; i <= symbols; ++i) {
 
-    snprintf(symbol, sizeof(symbol), "Symbol %d", i);
+    QString symbol = QString("Symbol %1").arg(i);
 
     OVL_SYMBOL_TYP type = (OVL_SYMBOL_TYP) inifile_readint_def(inifile, symbol, "Typ", 0);
     int points = inifile_readint_def(inifile, symbol, "Punkte", -1);
@@ -113,7 +112,6 @@ ggv_ovl_read()
     QString lon;
     switch (type) {
 
-      char coord[32];
       Waypoint* wpt;
       int group;
 
@@ -138,25 +136,23 @@ ggv_ovl_read()
           trk->rte_name = QString("Track %1").arg(track_ct);
         }
 
-        for (int j = 0; j < points; j++) {
+        for (int j = 0; j < points; ++j) {
 
           wpt = new Waypoint;
 
-          snprintf(coord, sizeof(coord), "YKoord%d", j);
-          lat = inifile_readstr(inifile, symbol, coord);
-          if (!lat.isNull()) {
-            wpt->latitude = lat.toDouble();
-          } else {
+          lat = inifile_readstr(inifile, symbol, QString("YKoord%1").arg(j));
+          if (lat.isNull()) {
+            delete wpt;
             continue;
           }
+          wpt->latitude = lat.toDouble();
 
-          snprintf(coord, sizeof(coord), "XKoord%d", j);
-          lon = inifile_readstr(inifile, symbol, coord);
-          if (!lon.isNull()) {
-            wpt->longitude = lon.toDouble();
-          } else {
+          lon = inifile_readstr(inifile, symbol, QString("XKoord%1").arg(j));
+          if (lon.isNull()) {
+            delete wpt;
             continue;
           }
+          wpt->longitude = lon.toDouble();
 
           if (group > 1) {
             route_add_wpt(rte, wpt);
@@ -174,17 +170,17 @@ ggv_ovl_read()
       wpt->shortname = symbol;
 
       lat = inifile_readstr(inifile, symbol, "YKoord");
-      if (!lat.isNull()) {
-        wpt->latitude = lat.toDouble();
-      } else {
+      if (lat.isNull()) {
+        delete wpt;
         continue;
       }
+      wpt->latitude = lat.toDouble();
       lon = inifile_readstr(inifile, symbol, "XKoord");
-      if (!lon.isNull()) {
-        wpt->longitude = lon.toDouble();
-      } else {
+      if (lon.isNull()) {
+        delete wpt;
         continue;
       }
+      wpt->longitude = lon.toDouble();
 
       waypt_add(wpt);
       break;
index 0c385c2304abea08764e6542e0a3cdc2fc018d80..624f779acecfbf03dc987fca3ed12f700a757028 100644 (file)
@@ -204,9 +204,9 @@ inifile_done(inifile_t* inifile)
 }
 
 bool
-inifile_has_section(const inifile_t* inifile, const char* section)
+inifile_has_section(const inifile_t* inifile, const QString& section)
 {
-  return inifile->sections.contains(QString(section).toLower());
+  return inifile->sections.contains(section.toLower());
 }
 
 /*
@@ -248,7 +248,7 @@ inifile_readint(const inifile_t* inifile, const QString& section, const QString&
  */
 
 int
-inifile_readint_def(const inifile_t* inifile, const char* section, const char* key, const int def)
+inifile_readint_def(const inifile_t* inifile, const QString& section, const QString& key, const int def)
 {
   int result;
 
index f428caa93f1a6a38069be000cf0872f46aabc79a..5706775af150e4e6cbf64ada38248ec9d3f509ba 100644 (file)
--- a/inifile.h
+++ b/inifile.h
@@ -39,7 +39,7 @@ struct inifile_t {
 inifile_t* inifile_init(const QString& filename, const char* myname);
 void inifile_done(inifile_t* inifile);
 
-bool inifile_has_section(const inifile_t* inifile, const char* section);
+bool inifile_has_section(const inifile_t* inifile, const QString& section);
 
 /*
      inifile_readstr:
@@ -59,6 +59,6 @@ int inifile_readint(const inifile_t* inifile, const QString& section, const QStr
      inifile_readint_def:
        if found inifile_readint_def returns value of key, otherwise a default value "def"
  */
-int inifile_readint_def(const inifile_t* inifile, const char* section, const char* key, int def);
+int inifile_readint_def(const inifile_t* inifile, const QString& section, const QString& key, int def);
 
 #endif